﻿using UnityEngine;
using System.Collections;

public class DikeSpawnerPart : MonoBehaviour 
{
    [SerializeField]
    private GameObject dike;
    [SerializeField]
    private Vector2 speed = new Vector2(2, 3);
    //[SerializeField]
    //private float scale = 15;

    public Transform spawner0;
    public Transform spawner1;

    private float timer = 0;
    private float time;
    private GameController_ScoreManager scoreManager;
    private GameController_Stream stream;


    void Awake()
    {
        time = Random.Range(speed.x, speed.y);
        scoreManager = FindObjectOfType<GameController_ScoreManager>();
        stream = FindObjectOfType<GameController_Stream>();
    }
    void Update()
    {
        //Right
        if (timer > time)
        {
            time = Random.Range(speed.x, speed.y);
            timer = 0;
            GameObject clone = Instantiate(
                dike,
                spawner0.position,
                spawner0.rotation
                ) as GameObject;

            clone.transform.localScale = new Vector3(-clone.transform.localScale.x, clone.transform.localScale.y, clone.transform.localScale.z);

            clone.GetComponent<DikeMover>().scoreManager = scoreManager;
            clone.GetComponent<DikeMover>().SetVelosity(stream.speed);
        }
        else
        {
            timer += Time.deltaTime;
        }

        //left
        if (timer > time)
        {
            time = Random.Range(speed.x, speed.y);
            timer = 0;
            GameObject clone = Instantiate(
                dike,
                spawner1.position,
                spawner1.rotation
                ) as GameObject;

            //clone.transform.localScale = new Vector3(-clone.transform.localScale.x, clone.transform.localScale.y, clone.transform.localScale.z);

            clone.GetComponent<DikeMover>().scoreManager = scoreManager;
            clone.GetComponent<DikeMover>().SetVelosity(stream.speed);
        }
        else
        {
            timer += Time.deltaTime;
        }
    }
    
}
